<?php
/**
 * Created by YuanXiShop.
 * User: YuanXi
 * Date: 2021/6/4 0004
 * Time: 17:12
 */

namespace home;


use JasonGrimes\Paginator;
use models\BaseDao;

class Article extends Home
{
    function index($id)
    {
        $db = new BaseDao();

        $article = $db->get('article','*',['id'=>$id]);

        //获取分类数据

        $class = $db->select('class','*',['ORDER'=>['ord'=>'ASC','id'=>'ASC']]);

        $classes = [];

        foreach ($class as $v){
            $classes[$v['id']] = $v['catname'];
        }

        $catname = $classes[$article['cid']];

        $position = " &gt; 资讯中心 &gt; <a href='/alist/{$article['cid']}'>{$classes[$article['cid']]}</a> &gt; {$article['name']}";

        //相关文章

        $data = $db->select('article',['id','name','atime'],['cid'=>$article['cid'],'LIMIT'=>6]);

        $this->assign('data',$data);


        $this->assign('catname',$catname);

        $this->assign('position',$position);

        $this->assign($article);

        $this->assign('class',$class);

        $this->assign('title',$article['name']);

        $this->display('article/index');

        $db->update('article', ['clicknum[+]'=>1], ['id'=>$id]);
    }

    function alist($cid = 0)
    {
        $db = new  BaseDao();

        //获取分类数据

        $class = $db->select('class','*',['ORDER'=>['ord'=>'ASC','id'=>'ASC']]);

        $classes = [];

        foreach ($class as $v){
            $classes[$v['id']] = $v['catname'];
        }

        $position = " &gt; 资讯中心 &gt; <a href='/alist/{$cid}'>{$classes[$cid]}</a> ";

        $catname = $classes[$cid];


        //主体数据

        $num = $_GET['num'] ?? 1;

        $where =[];
        //搜索条件
        if($cid != 0 ) {
            $where['cid'] = $cid;
        }


        $totalItems = $db->count('article',$where);//总页面数
        $itemsPerPage = PNUM;//每页显示多少条数据
        $currentPage = $num;//当前页是第几页
        $urlPattern = '/alist/'.$cid.'?num=(:num)';

        $paginator = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);

        //从第几页开始
        $start = ($currentPage-1) * $itemsPerPage;
        $where['LIMIT'] = [$start,$itemsPerPage];


        $data = $db->select('article','*',$where);
        // $data = $db->debug()->select('article','*',$where);

        //排序

        $this->assign('data',$data);
        $this->assign('fpage',$paginator);

        $this->assign('catname',$catname);

        $this->assign('position',$position);

        $this->assign('class',$class);

        $this->assign('title','');

        $this->display('article/alist');
    }
}